import type { MetadataField, MetadataFieldPropsSchema } from '../fields/maps';
import { getFieldMaps } from '../fields/maps/fieldMaps';
import { getFieldPropsMaps } from '../fields/maps/fieldPropsMaps';
import { cloneDeep, get } from 'lodash';
import type { Schema } from 'gantd/lib/schema-form';
/**
 * 字段库的公共方法
 */
const fields = getFieldMaps();
const filedsProps = getFieldPropsMaps();
const { defaultFieldProps, metadataFieldPropsMaps, requiredFieldProps } = filedsProps;
export function getFieldName(name: string) {
  if (name === 'RelateEntryPropCmp') return tr('关联动态对象');
  const field = getField(name);
  return field.name;
}

/**
 *
 *
 * @export
 * @param {string} name
 * @returns {MetadataField}
 */
export function getField(name: string): MetadataField {
  const fieldsArray = Object.keys(fields);
  for (const keyanme of fieldsArray) {
    const field = fields[keyanme];
    if (keyanme === name) return cloneDeep({ componentType: keyanme, ...field });
    if (fields[keyanme].extName === name) return cloneDeep({ componentType: keyanme, ...field });
  }
  console.warn(`${name}${tr('组件未定义')}`);
  return { componentType: name } as MetadataField;
}
export function getFieldProps(name: string) {
  const allFieldProps = { ...defaultFieldProps, ...metadataFieldPropsMaps, ...requiredFieldProps };
  const fieldsPropsArray = Object.keys(allFieldProps);
  for (const keyanme of fieldsPropsArray) {
    const fieldProps = allFieldProps[keyanme];
    if (keyanme === name) return cloneDeep({ reactPropsName: keyanme, ...fieldProps });
    if (fieldProps.aliasName === name) return cloneDeep({ reactPropsName: keyanme, ...fieldProps });
  }
  console.warn(`${name}${tr('属性未定义')}`);
  return {} as MetadataFieldPropsSchema;
}
export function getFieldPropsDedefaultConfig(name: string) {
  const newSchema: any = {};
  const newUiScema: any = {};
  const newDefalutValue: any = {};
  const field = getField(name);
  const propsArray = field.fieldProps ? field.fieldProps : [];
  propsArray.map((propsKey: string) => {
    const { aliasName, uiScema, defaultValue, ...schema } = metadataFieldPropsMaps[propsKey];
    const cloneSchema = cloneDeep(schema);
    const keyname = aliasName ? aliasName : propsKey;
    if (uiScema) newUiScema[keyname] = cloneDeep(uiScema);
    if (defaultValue !== undefined) newDefalutValue[keyname] = cloneDeep(defaultValue);
    newSchema[keyname] = cloneSchema;
  });
  return {
    uiScema: newUiScema,
    schema: {
      ...cloneDeep(defaultFieldProps),
      ...newSchema,
    },
    defaultValue: newDefalutValue,
  };
}

export function diffFormVals(formValsT: any, dataT: any, formSchema: Schema): boolean {
  const diff = true;
  const formVals = formValsT ? formValsT : {};
  const data = dataT ? dataT : {};
  const arr = Object.keys(formVals);
  for (const keyname of arr) {
    if (formVals[keyname] && typeof formVals[keyname] === 'object' && !Array.isArray(formVals[keyname])) {
      return diffFormVals(formVals[keyname], data[keyname], get(formSchema, `propertyType.${keyname}`));
    } else {
      if (get(formSchema, `propertyType.${keyname}`, undefined) == undefined) break;
      if (get(data, `${keyname}`, undefined) !== get(formVals, `${keyname}`, undefined)) return false;
    }
  }
  return diff;
}
export function dataSourceNumber(dataSource: any[], beginIndex?: number) {
  const serial = beginIndex ? beginIndex + 1 : 1;
  return dataSource.map((item: any, index: number) => ({ ...item, index: serial + index }));
}
export function colunmsNumber(colunms: any[]) {
  return [{ fieldName: 'index', title: tr('序号'), width: 50 }, ...colunms];
}
